package com.zhuss.core.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.book.CreateBookBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.BookDao;
import com.zhuss.core.db.entity.Book;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.AabException;
import com.zhuss.util.JsonUtil;

/**
 * 创建Book processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("bookCreate")
public class BookCreate extends AbstractProcessor<Object, CreateBookBo> implements Processor<Object, CreateBookBo> {

    @Autowired
    private BookDao bookDao;
    
    @Autowired
	@Qualifier("bookRead")
	private BookRead bookRead;
    
    @Autowired
	@Qualifier("deptRead")
	private DeptRead deptRead;
    
    
    /**
     * 校验
     */
    @Override
    public void check(CreateBookBo bo) throws AabException {
    }
    
    /**
     * 操作方法
     * @throws AabException 
     */
    @Override
    public Object process(CreateBookBo param) throws AabException {
    	Book record = new Book();
    	record.setAge(param.getAge());
    	record.setCompanyId(param.getCompanyId());
    	record.setName(param.getName());
    	record.setOpenId(param.getOpenId());
    	record.setIsBind(param.getIsBind());
    	record.setPhone(param.getPhone());
    	record.setSex(param.getSex());
    	record.setType(param.getType());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	bookDao.create(record);
    	
    	Long bookId = record.getBookId();
    	param.setTargetId(bookId);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CreateBookBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.Book创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
